using FytSoa.Domain.Cms;
using FytSoa.Sugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using MapsterMapper;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Masuit.Tools.Linq;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Cms;

/// <summary>
/// 文章表 服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v2")]
public class CmsArticleService : SugarRepository<CmsArticle>,ICmsArticleService 
{
    private readonly IMapper _mapper;
    public CmsArticleService(IMapper mapper)
    {
        _mapper = mapper;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<ApiResult<PageResult<CmsArticleDto>>> GetPagesAsync(CmsArticleParam param)
    {
        var where = Expressionable.Create<CmsArticle>();
        if (!string.IsNullOrEmpty (param.Key)) {
            where.And (m => m.Title.Contains (param.Key) || m.KeyWord.Contains (param.Key) || m.SubTitle.Contains (param.Key) || m.Summary.Contains (param.Key) || m.Author.Contains (param.Key) || m.Source.Contains (param.Key) || m.Tag.Equals (param.Key));
        }
        if (param.Id != 0) {
            where.And (m => m.ColumnId.ToString()!.Contains(param.Id.ToString()));
        }
        switch (param.Status)
        {
            case "1":
                @where.And (m => m.Status);
                break;
            case "2":
                @where.And (m => !m.Status);
                break;
        }
        if (param.Attr != 0 && param.Attr!=null) {
            where.And (m => m.Attr.ToString()!.Contains (param.Attr.ToString() ?? string.Empty));
        }
        var (list,totalItems,totalPage) = await base.GetPageResultAsync(where.ToExpression(), param.Page, param.Limit, m => m.Id);
        var result = new PageResult<CmsArticleDto>()
        {
            Items = _mapper.Map<List<CmsArticleDto>>(list),
            TotalItems = totalItems,
            TotalPages = totalPage
        };
        return JResult<PageResult<CmsArticleDto>>.Success(result);
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ApiResult<CmsArticleDto>> GetAsync(long id)
    {
        return JResult<CmsArticleDto>.Success(_mapper.Map<CmsArticleDto>(await base.GetByIdAsync(id)));
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<ApiResult<bool>> AddAsync(CmsArticleDto model)
    {
        model.Id = Unique.Id();
        return JResult<bool>.Success(await base.InsertAsync(_mapper.Map<CmsArticle>(model)));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<ApiResult<bool>> ModifyAsync(CmsArticleDto model)
    {
        return JResult<bool>.Success(await base.UpdateAsync(_mapper.Map<CmsArticle>(model)));
    }

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<ApiResult<bool>> DeleteAsync(string ids)
    {
        return JResult<bool>.Success(await base.DeleteAsync(m=>ids.StrToListLong().Contains(m.Id)));
    }
    
    /// <summary>
    /// 添加到回收站
    /// </summary>
    /// <returns></returns>
    public async Task<ApiResult<bool>> UpdateRecycleAsync(List<long> ids)
    {
        var list = await base.GetListAsync (m => ids.Contains (m.Id));
        list.ForEach (m => {
            m.Attr.Add(5);
        });
        return JResult<bool>.Success(await base.UpdateRangeAsync(list));
    }
    
    /// <summary>
    /// 回收站恢复
    /// </summary>
    /// <returns></returns>
    public async Task<ApiResult<bool>> UpdateRecoveryAsync(List<long> ids)
    {
        var list = await base.GetListAsync (m => ids.Contains (m.Id));
        foreach (var item in list)
        {
            item.Attr.RemoveAll(m => m == 5);
        }
        return JResult<bool>.Success(await base.UpdateRangeAsync(list));
    }
}
